package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Intent;
import android.content.IntentSender;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.FileUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.UriUtils;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.databinding.ActivityImgDetailBinding;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.UriUtil;

public class ImgDetailActivity extends BaseAc<ActivityImgDetailBinding> {

    public static boolean isLocal;
    public static String path;
    public final int REQUEST_CODE_DELETE_IMAGE = 1000;


    @Override
    protected int onCreate() {
        return R.layout.activity_img_detail;
    }

    @Override
    protected boolean isStatusBarTextDark() {
        return false;
    }

    @Override
    protected void initView() {
        mDataBinding.ivDetailBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.ivDetailDelete.setOnClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivDetailDelete://删除
                if (isLocal) {
                    deletePicture();
                } else {
                    delete();
                }
                break;
        }
    }

    @Override
    protected void initData() {
        Glide.with(mContext).load(path).into(mDataBinding.ivDetailImg);
    }

    //适配android11以上删除相册文件方式
    private void deletePicture() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
            List<Uri> uris = new ArrayList<>(); // 这里存放所有需要删除的图片的uri
            uris.add(getUri());
            PendingIntent pi = MediaStore.createDeleteRequest(getContentResolver(), uris);
            try {
                startIntentSenderForResult(pi.getIntentSender(), REQUEST_CODE_DELETE_IMAGE, null, 0, 0, 0);
            } catch (IntentSender.SendIntentException e) {
                Log.e("TAG", "startIntentSender error:" + e.toString());
            }
        } else {
            getContentResolver().delete(Uri.parse(path), null, null);
            finish();
        }
    }

    //删除
    private void delete() {
        showDialog(getString(R.string.delete_ing));
        RxUtil.create(new RxUtil.Callback<String>() {

            @Override
            public void doBackground(ObservableEmitter<String> emitter) {
                FileUtils.delete(path);
                emitter.onNext("");
            }

            @Override
            public void accept(String path) {
                dismissDialog();
                ToastUtils.showShort(getString(R.string.delete_suc));
                finish();
            }
        });
    }

    private Uri getUri() {
        ContentResolver resolver = this.getContentResolver();     // 通过context上下文可拿到

        Cursor cursor = MediaStore.Images.Media.query(resolver,

                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,

                new String[]{MediaStore.Images.Media._ID},

                MediaStore.Images.Media.DATA + "=?",

                new String[]{path}, null);

        if (null != cursor && cursor.moveToFirst()) {

            Long id = cursor.getLong(0);

            Uri contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

            Uri uri = ContentUris.withAppendedId(contentUri, id);
            return uri;
        }
        return null;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_DELETE_IMAGE) {
            if (resultCode == Activity.RESULT_OK) {
                finish();
            }
        }
    }

}